package com.zhkc.iess.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.zhkc.iess.auth.api.AuthApi;
import com.zhkc.iess.auth.dto.LoginInfoDTO;
import com.zhkc.iess.auth.service.AuthService;
import com.zhkc.iess.auth.vo.LoginInfoVO;
import com.zhkc.wisdom.common.pojo.vo.ResultVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class AuthController implements AuthApi {

    @Autowired
    private AuthService authService;


    @Override
    public ResultVO<LoginInfoVO> login(@RequestBody @Valid LoginInfoDTO dto) {
        //todo,前后端密码传输暂时没有加密
        // todo 登录之后暂时没有返回角色列表 和 用户头像
        return ResultVO.success(authService.login(dto));
    }


    @Override
    public ResultVO<Void> loginOut() {
        StpUtil.logout();
        return ResultVO.success();
    }

    @Override
    public ResultVO<Void> test() {

        Object name = StpUtil.getExtra("name");

        System.out.println("获取当前会话账号name: " + name);

        Object loginId = StpUtil.getLoginId();
        System.out.println("获取当前会话账号id: " + loginId);

        // 获取当前会话的 token 值
        String tokenValue = StpUtil.getTokenValue();
        System.out.println("获取当前会话的 token 值: " + tokenValue);

        String tokenName = StpUtil.getTokenName();
        System.out.println("tokenName: " + tokenName);

        long tokenTimeout = StpUtil.getTokenTimeout();
        System.out.println("获取当前会话剩余有效期（单位：s，返回-1代表永久有效）: " + tokenTimeout);

        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println("获取当前会话的 token 信息参数: " + tokenInfo);

        StpUtil.login(StpUtil.getLoginId(), 3600);
        System.out.println("延长时间: " + StpUtil.getTokenTimeout());

        return ResultVO.success();
    }


}
